/**
 * 
 */
package com.wubo.sec.init;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractXmlApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author wubo
 * @CreateDate 2010-8-17
 * @version 1.0.01
 */
public abstract class SpringInitExecutor implements InitExecutor {
	
	public void init(){
		String[] defaultConfig = getDefaultConfigLocations();
		String[] userConfig = getUserConfigLocations();
		int dcfg = defaultConfig.length;
		int ucfg = userConfig.length;
		String[] config = new String[dcfg + ucfg];
		
		System.arraycopy(defaultConfig, 0, config, 0, dcfg);
		System.arraycopy(userConfig, 0, config, dcfg, ucfg);
		
		AbstractXmlApplicationContext ctx = new ClassPathXmlApplicationContext(config);
		ctx.registerShutdownHook();
		
		try{
			doInit(ctx);
		}finally{
			ctx.close();
		}
	}
	
	protected abstract void doInit(ApplicationContext ctx);

	protected String[] getDefaultConfigLocations() {
		return new String[]{"classpath*:applicationContext.xml",
			"classpath*:sec/security-service.xml"};
	}

	protected String[] getUserConfigLocations() {
		return new String[]{};
	}
	
}
